package chapter01.v5;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {

	private String _name;

	private Vector<Rental> _rentals = new Vector<Rental>();

	public String getName() {
		return _name;
	}

	public void setName(String _name) {
		this._name = _name;
	}

	public Vector<Rental> getRentals() {
		return _rentals;
	}

	public void setRentals(Vector<Rental> _rentals) {
		this._rentals = _rentals;
	}

	public String statement() {

		Enumeration<Rental> rentals = _rentals.elements();

		String result = "Rental Record for " + getName() + "\n";

		while (rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(each.getCharge()) + "\n";
		}

		result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
		result += "You earned " + String.valueOf(getFrequentRenterPoints()) + " frequent renter points";

		return result;
	}

	// TODO 重构降低了性能，多循环了几次
	private double getTotalCharge() {
		double result = 0; // 总消费金额
		Enumeration<Rental> rentals = _rentals.elements();
		while (rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			result += each.getCharge();
		}
		return result;
	}

	private int getFrequentRenterPoints() {
		int result = 0;
		Enumeration<Rental> rentals = _rentals.elements();
		while (rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();
			result += each.getFrequentRenterPoints();
		}
		return result;
	}

}
